import 'dart:convert';

import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:flutter_chip/config/Api.dart';
import 'package:flutter_chip/config/Comm.dart';
import 'package:flutter_chip/config/Http.dart';
import 'package:flutter_chip/config/Routers.dart';
import 'package:flutter_chip/view/mine/wallet/view_tcash.dart';
import 'package:flutter_chip/widget/RouteSlide.dart';
import 'package:flutter_lifecycle_state/flutter_lifecycle_state.dart';
import 'package:fsuper/fsuper.dart';

///我的钱包
class WalletPage extends StatefulWidget {
  @override
  _WalletPageState createState() => _WalletPageState();
}

class _WalletPageState extends StateWithLifecycle<WalletPage> {
  String money = "0.00";

  @override
  void onResume() {
    super.onResume();
    _getWalletInfo();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
          icon: Icon(Icons.arrow_back, color: Theme.of(context).cardColor),
          onPressed: () => Routers.router.pop(context),
        ),
        title: Text(
          "我的钱包",
          style: TextStyle(fontSize: 18, color: Theme.of(context).cardColor),
        ),
        titleSpacing: 0,
        elevation: 0,
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          _moneyWidget(),
          _takeCashWidget(),
          Container(
            margin: EdgeInsets.only(left: 48),
            color: Color(0xffDDDDDD),
            height: 0.5,
          ),
//          _rechargeWidget(),
//          Container(
//            margin: EdgeInsets.only(left: 48),
//            color: Color(0xffDDDDDD),
//            height: 0.5,
//          ),
          _walletInfoWidget(),
        ],
      ),
    );
  }

  ///钱包余额卡片
  Widget _moneyWidget() {
    return Container(
      width: Comm.screenWidth,
      padding: EdgeInsets.only(top: 10, left: 20, bottom: 20),
      color: Theme.of(context).accentColor,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Text(
            "账户余额（元）",
            style: TextStyle(
              color: Color(0xffDDDDDD),
            ),
          ),
          SizedBox(height: 30),
          Text(
            double.parse(money).toStringAsFixed(2) ?? "0.00",
            style: TextStyle(
              color: Color(0xffEEEEEE),
              fontSize: 36,
              fontWeight: FontWeight.w600,
            ),
          ),
          SizedBox(height: 10),
        ],
      ),
    );
  }

  ///提取现金
  Widget _takeCashWidget() {
    return Container(
      padding: EdgeInsets.only(left: 10, right: 10),
      child: FSuper(
        onClick: () {
          Navigator.of(context).push(RouteSlide(
            TakeCashPage(totalMoney: money),
          ));
        },
        height: 48,
        width: double.infinity,
        backgroundColor: Colors.white,
        padding: EdgeInsets.only(left: 40, right: 20),
        textAlignment: Alignment.centerLeft,
        text: "提现",
        textSize: 16,
        child1: Icon(
          Icons.credit_card,
          color: Color(0xffF19C2A),
          size: 28,
        ),
        child1Alignment: Alignment.centerLeft,
        child2: Image.asset(Comm.IMG_ARROW_RIGHT, width: 18, height: 18),
        child2Alignment: Alignment.centerRight,
      ),
    );
  }

  ///充值
  Widget _rechargeWidget() {
    return Container(
      padding: EdgeInsets.only(left: 10, right: 10),
      child: FSuper(
        onClick: () {},
        height: 48,
        width: double.infinity,
        backgroundColor: Colors.white,
        padding: EdgeInsets.only(left: 40, right: 20),
        textAlignment: Alignment.centerLeft,
        text: "充值",
        textSize: 16,
        child1: Icon(
          Icons.account_balance_wallet,
          color: Color(0xff088EEE),
          size: 28,
        ),
        child1Alignment: Alignment.centerLeft,
        child2: Image.asset(Comm.IMG_ARROW_RIGHT, width: 18, height: 18),
        child2Alignment: Alignment.centerRight,
      ),
    );
  }

  ///账户明细
  Widget _walletInfoWidget() {
    return Container(
      padding: EdgeInsets.only(left: 10, right: 10),
      child: FSuper(
        onClick: () {
          Routers.router.navigateTo(
            context,
            Routers.PAGE_TRADE,
            transition: TransitionType.inFromRight,
          );
        },
        height: 48,
        width: double.infinity,
        backgroundColor: Colors.white,
        padding: EdgeInsets.only(left: 40, right: 20),
        textAlignment: Alignment.centerLeft,
        text: "账户明细",
        textSize: 16,
        child1: Icon(
          Icons.event_note,
          color: Color(0xffED5213),
          size: 28,
        ),
        child1Alignment: Alignment.centerLeft,
        child2: Image.asset(Comm.IMG_ARROW_RIGHT, width: 18, height: 18),
        child2Alignment: Alignment.centerRight,
      ),
    );
  }

  ///钱包信息
  void _getWalletInfo() {
    Http.get(Api.URL_WALLET_INFO, successCallBack: (data) {
      setState(() {
        money = "${json.decode(data)["availMoney"]}";
      });
    });
  }
}
